package org.csu.pet_store_2.Service;


import com.alibaba.fastjson.JSONObject;
import com.aliyun.credentials.http.MethodType;
import com.aliyun.credentials.utils.StringUtils;
import com.aliyun.tea.*;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.ibatis.javassist.tools.rmi.Sample;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class Sendmessage {
    public void smsMessage(String Phonenum,String authcode){  //AccessKey ID 账号 AccessKey secret密码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tGcvgFKyvX1PJVeTkv6", "AG0NQZspB2Bdx6YNg77N0W6GXIJwnY");
        IAcsClient client = new DefaultAcsClient(profile);
        //阿里云原有的配置信息我们不需要改动
        CommonRequest request = new CommonRequest();
        request.setSysMethod(com.aliyuncs.http.MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        //自定义短信信息
        request.putQueryParameter("PhoneNumbers", Phonenum); //发送给哪个手机号
        request.putQueryParameter("SignName", "梁泉宠物商店");  //自己配置的短信签名
        request.putQueryParameter("TemplateCode", "SMS_275060549"); //自己配置的模板 模版CODE

        //构建一个短信验证码
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",authcode);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));   //转换成json字符串
        try {
            CommonResponse response = client.getCommonResponse(request); //发送至客户端
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}